### Table 1: knowledge of CV laws in Brazil from survey

library(haven)

## Clear workspace

rm(list=ls())


##Opening the data:

load("ReplicationData.RData")


##Frequencies of knowledge questions: 1 correct answer, 0 otherwise

print_labels(SurveyData$P5A.1)
knowledge1 <- table(as.numeric(SurveyData$P5A.1 == 1))
prop.table(knowledge1)*100

print_labels(SurveyData$P5B.1)
knowledge3 <- table(as.numeric(SurveyData$P5B.1 == 2))
prop.table(knowledge3)*100


## Knowledge among young voters (18-24):

DataYoung <- subset(SurveyData, age<25)

##Frequencies of knowledge questions: 1 correct answer, 0 otherwise

DataYoung$know1 <- as.numeric(DataYoung$P5A.1 == 1)
know1Young <- table(DataYoung$know1)
prop.table(know1Young)*100

DataYoung$know3 <- as.numeric(DataYoung$P5B.1 == 2)
know3Young <- table(DataYoung$know3)
prop.table(know3Young)*100


## Knowledge by education:

## High school

DataHS <- subset(SurveyData, (P3>=7))

##Frequencies of knowledge questions: 1 correct answer, 0 otherwise

DataHS$know1 <- as.numeric(DataHS$P5A.1 == 1)
know1HS <- table(DataHS$know1)
prop.table(know1HS)*100

DataHS$know3 <- as.numeric(DataHS$P5B.1 == 2)
know3HS <- table(DataHS$know3)
prop.table(know3HS)*100


## No high school

DatanoHS <- subset(SurveyData, (P3<7))

##Frequencies of knowledge questions: 1 correct answer, 0 otherwise

DatanoHS$know1 <- as.numeric(DatanoHS$P5A.1 == 1)
know1noHS <- table(DatanoHS$know1)
prop.table(know1noHS)*100

DatanoHS$know3 <- as.numeric(DatanoHS$P5B.1 == 2)
know3noHS <- table(DatanoHS$know3)
prop.table(know3noHS)*100


## HS vs no HS: binomial proportion test

## Knowledge item 1:

Edup11 <- know1HS[2]/sum(know1HS)
Edun11 <- sum(know1HS)

Edup21 <- know1noHS[2]/sum(know1noHS)
Edun21 <- sum(know1noHS)

Edup1 <- (Edun11 * Edup11 + Edun21 * Edup21)/ (Edun11 + Edun21)
Eduz1 <- (Edup11 - Edup21) / sqrt(Edup1 * (1-Edup1) * (1/Edun11 + 1/Edun21))
Eduz1

(1 - pnorm(Eduz1, 0, 1))*2

## Knowledge item 3:

Edup13 <- know3HS[2]/sum(know3HS)
Edun13 <- sum(know3HS)

Edup23 <- know3noHS[2]/sum(know3noHS)
Edun23 <- sum(know3noHS)

Edup3 <- (Edun13 * Edup13 + Edun23 * Edup23)/ (Edun13 + Edun23)
Eduz3 <- (Edup13 - Edup23) / sqrt(Edup3 * (1-Edup3) * (1/Edun13 + 1/Edun23))
Eduz3

(1 - pnorm(Eduz3, 0, 1))*2



## Gender differences: Online Appendix section S5.1

print_labels(SurveyData$sex)

## Knowledge by gender among young voters:

## Women

DataWomen <- subset(SurveyData, (sex == 2 & age<25))

##Frequencies of knowledge questions: 1 correct answer, 0 otherwise

DataWomen$know1 <- as.numeric(DataWomen$P5A.1 == 1)
know1Women <- table(DataWomen$know1)
prop.table(know1Women)*100

DataWomen$know3 <- as.numeric(DataWomen$P5B.1 == 2)
know3Women <- table(DataWomen$know3)
prop.table(know3Women)*100


## Men

DataMen <- subset(SurveyData, (sex == 1 & age<25))

##Frequencies of knowledge questions: 1 correct answer, 0 otherwise

DataMen$know1 <- as.numeric(DataMen$P5A.1 == 1)
know1Men <- table(DataMen$know1)
prop.table(know1Men)*100

DataMen$know3 <- as.numeric(DataMen$P5B.1 == 2)
know3Men <- table(DataMen$know3)
prop.table(know3Men)*100


## Women vs Men: binomial proportion test

## Knowledge item 1:

Edup11 <- know1Women[2]/sum(know1Women)
Edun11 <- sum(know1Women)

Edup21 <- know1Men[2]/sum(know1Men)
Edun21 <- sum(know1Men)

Edup1 <- (Edun11 * Edup11 + Edun21 * Edup21)/ (Edun11 + Edun21)
Eduz1 <- (Edup11 - Edup21) / sqrt(Edup1 * (1-Edup1) * (1/Edun11 + 1/Edun21))
Eduz1

(1 - pnorm(Eduz1, 0, 1))*2

## Knowledge item 3:

Edup13 <- know3Women[2]/sum(know3Women)
Edun13 <- sum(know3Women)

Edup23 <- know3Men[2]/sum(know3Men)
Edun23 <- sum(know3Men)

Edup3 <- (Edun13 * Edup13 + Edun23 * Edup23)/ (Edun13 + Edun23)
Eduz3 <- (Edup13 - Edup23) / sqrt(Edup3 * (1-Edup3) * (1/Edun13 + 1/Edun23))
Eduz3

(1 - pnorm(Eduz3, 0, 1))*2

